package edu.buaa.act.travel.m.tpdl;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import edu.buaa.act.travel.m.tpdl.moveaction.MoveAction;
import edu.buaa.act.travel.m.tpdl.moveaction.MoveActionFactory;


public class TrafficActivity extends TransitActivity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String TYPE = "traffic";
	private String startPlace;
	private String endPlace;
	String city;
	int time;	// minute
	int length;	// meter
	int walk;	
	List<MoveAction> actions;
	
	@Override
	protected JSONObject toJSONObject(){
		JSONObject o = super.toJSONObject();
		try {
			o.put("startPlace", startPlace);
			o.put("endPlace", endPlace);
			o.put("city", city);
			o.put("type", TYPE);
			o.put("time", time);
			o.put("length", length);
			o.put("walk", walk);
			o.put("actions", actions);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return o;
	}

	@Override
	public void fromJSON(JSONObject j) throws JSONException {
		super.fromJSON(j);
		if(j.has("startPlace")){
			this.setStartPlace(j.getString("startPlace"));
		}
		if(j.has("endPlace")){
			this.setEndPlace(j.getString("endPlace"));
		}
		if(j.has("city")){
			this.setCity(j.getString("city"));
		}
		if(j.has("time")){
			this.setTime(j.getInt("time"));
		}
		if(j.has("length")){
			this.setLength(j.getInt("length"));
		}
		if(j.has("walk")){
			this.setWalk(j.getInt("walk"));
		}
		MoveActionFactory factory = MoveActionFactory.getInstance();
		
		JSONArray array = j.getJSONArray("actions");
		for (int i = 0; i < array.length(); i++) {
			JSONObject o = array.getJSONObject(i);
			MoveAction a = factory.create(o.getString("type"));
			a.fromJSON(o);
			this.addMoveAction(a);
		}
	}
	
	@Override
	public String toJSONString(){
		JSONObject o = toJSONObject();
		return o.toString();
	}
	
	@Override
	public String toString() {
		String des = "";
		for(MoveAction a : actions){
			des += a.toString()+"\n";
		}
		return des;
	}
	
	public void addMoveAction(MoveAction a){
		if(actions == null){
			actions = new ArrayList<MoveAction>();
		}
		actions.add(a);
	}
	
	public void removeMoveAction(MoveAction a){
		actions.remove(a);
	}

	public String getStartPlace() {
		return startPlace;
	}

	public void setStartPlace(String startPlace) {
		this.startPlace = startPlace;
	}

	public String getEndPlace() {
		return endPlace;
	}

	public void setEndPlace(String endPlace) {
		this.endPlace = endPlace;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public int getTime() {
		return time;
	}

	public void setTime(int time) {
		this.time = time;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public int getWalk() {
		return walk;
	}

	public void setWalk(int walk) {
		this.walk = walk;
	}

}
